using System;
using System.IO;
using EnvDTE;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench.Resolvers;
using Microsoft.WowAddonStudio.Tests.Common.Mocks;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.WowBench
{
	/// <summary>
	/// 
	/// </summary>
	[TestFixture]
	public sealed class ExpressionResolverTest : WoWBenchTestBase
	{
		private ICommandResolver resolver;
		private const string InLineExpression = "print(10 + b);";
		private const string MultiLineExpression = @"b = 15 * k;
														print(10 + b);
														print(10 + b * (b + 5));";

		#region SetUp

		/// <summary>
		/// Tests the fixture set up.
		/// </summary>
		[TestFixtureSetUp]
		public override void TestFixtureSetUp()
		{
			base.TestFixtureSetUp();

			var props = new MockProperties(DTE, null);

			if (!props.ContainsKey("FullPath"))
				props.Add("FullPath", CreateMockProperty(props, "FullPath", Environment.CurrentDirectory));
			var project = new MockOAProject(DTE, props);

			SimpleTestServiceLocator.Register<EnvDTE.Project>(project);
		}

		/// <summary>
		/// Sets up.
		/// </summary>
		[SetUp]
		public override void SetUp()
		{
			resolver = new ExpressionResolver(Provider);
			foreach (string file in  Directory.GetFiles(Environment.CurrentDirectory, "*.lua"))
				File.Delete(file);
		}

		#endregion

		#region TearDown

		/// <summary>
		/// Tears down.
		/// </summary>
		[TearDown]
		public override void TearDown()
		{
			resolver.Reset();
			Assert.IsEmpty(Directory.GetFiles(Environment.CurrentDirectory, "*.lua"));
		}

		#endregion

		#region Tests

		/// <summary>
		/// Test_s the null expression.
		/// </summary>
		[Test(Description = "")]
		[ExpectedException(typeof (ArgumentNullException))]
		public void Test_NullExpression()
		{
			resolver.Resolve(null);
		}

		/// <summary>
		/// Test_s the in line expression.
		/// </summary>
		[Test(Description = "")]
		public void Test_InLineExpression()
		{
			string expression = resolver.Resolve(InLineExpression);
			Assert.AreEqual(InLineExpression, expression);
		}

		/// <summary>
		/// Test_s the multi line expression.
		/// </summary>
		[Test(Description = "")]
		public void Test_MultiLineExpression()
		{
			string expression = resolver.Resolve(MultiLineExpression);

			Console.WriteLine(expression);
			Assert.IsNotNull(expression);
			Assert.AreNotEqual(MultiLineExpression, expression);
			Assert.IsTrue(expression.Contains(".lua"));

			string fileName = expression.Substring(expression.IndexOf("\"") + 1,
			                                       expression.LastIndexOf("\"") - expression.IndexOf("\"") - 1);
			Assert.IsTrue(File.Exists(Path.Combine(Environment.CurrentDirectory, fileName)));
			string text = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, fileName));
			Assert.AreEqual(MultiLineExpression, text);
		}

		#endregion
	}
}